package com.music.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.music.server.pojo.*;
import com.music.server.mapper.CommentMapper;
import com.music.server.pojo.Comment;
import com.music.server.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.server.service.ConsumerService;
import com.music.server.service.SongListService;
import com.music.server.service.SongService;
import com.music.server.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-07
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private SongListService songListService;

    @Autowired
    private SongService songService;

    @Autowired
    private ConsumerService consumerService;

    @Override
    public Map<String, Object> page(Integer pageNum, Integer size, CommentVo commentVo) {
        Page<Comment> commentPage = new Page<>(pageNum,size);
        QueryWrapper<Comment> CommentQueryWrapper = new QueryWrapper<>();
        if (commentVo.getUserId()!=null){
            CommentQueryWrapper.eq("user_id",commentVo.getUserId());
        }
        if (commentVo.getContent()!=null){
            CommentQueryWrapper.like("content",commentVo.getContent());
        }
        if (commentVo.getSongId()!=null){
            CommentQueryWrapper.eq("song_id",commentVo.getSongId());
        }
        if (commentVo.getSongListId()!=null){
            CommentQueryWrapper.eq("song_list_id",commentVo.getSongListId());
        }
        if (commentVo.getType()!=null){
            CommentQueryWrapper.eq("type",commentVo.getType());
        }
        this.page(commentPage,CommentQueryWrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("total",commentPage.getTotal());
        List<Comment> commentList = commentPage.getRecords();
        for (Comment comment : commentList) {

            Consumer consumer = consumerService.getById(comment.getUserId());
            if (consumer!=null){
                comment.setUsername(consumer.getUsername());
            }
            Song song = songService.getById(comment.getSongId());
            if (song!=null){
                comment.setSongName(song.getName());
            }
            SongList songList = songListService.getById(comment.getSongListId());
            if (songList!=null) {
                comment.setSongListName(songList.getStyle()+"("+songList.getTitle()+")");
            }

        }
        data.put("list",commentList);
        return data;
    }
}
